package com.android.loveseason.game.view;

import com.android.loveseason.R;
import com.android.loveseason.game.layout.*;

import android.app.Activity;
import android.graphics.*;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
//import android.os.Bundle;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.util.*;

public class NumActivity extends Activity {
	private int img = R.drawable.grid_view_12;
	private Context mContext;
	private int numLines = 3;
	private Bitmap[][] bitA = new Bitmap[numLines + 1][numLines + 1];
	private ImageView[][] imgA = new ImageView[numLines + 1][numLines + 1];
	
	 
	@Override
	public void onCreate(Bundle b)
	{
		super.onCreate(b);
		mContext = this;
		Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(), img);
		int width = imgBitmap.getWidth();
		int height = imgBitmap.getHeight();
		int blockWidth = width / (numLines + 1);
		int blockHeight = height / (numLines + 1);
		Matrix matrix = new Matrix();
		DisplayMetrics dis = getResources().getDisplayMetrics();
		float scaleW = dis.widthPixels / width;
		float scaleH = dis.heightPixels / height;
		matrix.postScale(scaleW, scaleH);
		for (int i = 0; i < numLines + 1; i++)
		{
			for (int j = 0; j < numLines + 1; j++)
			{
				int x = j * blockWidth;
				int y = i * blockHeight;
				
				Bitmap mapi = Bitmap.createBitmap(imgBitmap, x, y, blockWidth, blockHeight, matrix, true);
				bitA[i][j] = mapi;
			}
		}
		for (int i = 0; i < numLines + 1; i++)
		{
			for (int j = 0; j < numLines + 1; j++)
			{
				ImageView imv = new ImageView(this);
				BitmapDrawable draw = new BitmapDrawable(bitA[i][j]);
				imv.setImageDrawable(draw);
				imgA[i][j] = imv;
			}
		}
		ImageLayout lay = new ImageLayout(this,imgA);
		Button b1 = new Button(this);
		b1.setText("hello");
		b1.setOnClickListener(OnClickBtn);
		lay.addView(b1);
		setContentView(lay);
	}
	private OnClickListener OnClickBtn = new OnClickListener(){

		@Override
		public void onClick(View v) {
			for (int i = 0; i < numLines + 1; i++)
			{
				for (int j = 0; j < numLines + 1; j++)
				{
					ImageView imv = new ImageView(mContext);
					BitmapDrawable draw = new BitmapDrawable(bitA[i][j]);
					imv.setImageDrawable(draw);
					imgA[j][i] = imv;
					bitA[i][j] = bitA[j][i];
				}
			}
			ImageLayout lay = new ImageLayout(mContext,imgA);
			Button b1 = new Button(mContext);
			b1.setText("hello");
			b1.setOnClickListener(OnClickBtn);
			lay.addView(b1);
			setContentView(lay);
		}
	};
	
}
